<?php 
use Wdl\helper\VillageHelper;
use Wdl\helper\webHelper;
 
defined('IN_IA') or exit('Access Denied');
global $_GPC, $_W;
load()->func('tpl'); 
$uniacid = webHelper::bindUniacid(); 
$openid = $_W['openid'];
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'xiaoqu';	
$template_name = sprintf("village/%s",  $operation);

$rd = !empty($_GPC['rd']) ? $_GPC['rd'] : 'index';	


// 小区管理
if ($operation == 'xiaoqu'){
    $type = 3;
    if ($rd == 'add'){

        if (checksubmit('submit')){

            $ret = VillageHelper::save_area_post($_GPC['id'], $type, $_GPC['data']);            
            message("操作成功", $this->createWebUrl('village', ['op'=> $operation]), 'success');

        }
        if(!empty($_GPC['id'])){
            $item = VillageHelper::get_area_data(intval($_GPC['id']));
        }

        $template_name = "village/xiaoqu_add";
    }else{

        $template_name = "village/xiaoqu";
    }
    //显示指定小区的地图lbs数据
}elseif($operation =='showlbsdata'){
   $id =  $_GPC['id'];
   $lbsdata = VillageHelper::has_lbs_data($id);

    include $this->template('village/lbsdata');
    exit;

}elseif($operation == 'quan'){
    $type = 2;
    if ($rd == 'add'){

        if (checksubmit('submit')){

            $ret = VillageHelper::save_area_post($_GPC['id'], $type, $_GPC['data']);            
            message("操作成功", $this->createWebUrl('village', ['op'=> $operation]), 'success');

        }
        if(!empty($_GPC['id'])){
            $item = VillageHelper::get_area_data(intval($_GPC['id']));
        }

        $template_name = "village/xiaoqu_add";
    }else{

        $template_name = "village/xiaoqu";
    }
}elseif($operation == 'savemap'){
    //保存地图坐标修改
    $save = [
        'point' => sprintf("%s,%s", $_GPC['lng'], $_GPC['lat']),
        'address' => $_GPC['address'],
    ];
    if($_GPC['lat'] && $_GPC['lng']){
        $village = pdo_fetch("select point from ".tablename("kbwdl_area")." where id=:id",['id'=> $_GPC['id']]);
        if($village['point'] !== $save['point'] ){
           
            VillageHelper::village_map_lbs_data($_GPC, $_GPC['id'], true);
        }
        
    }
    pdo_update('kbwdl_area', $save, ['id'=> $_GPC['id'], 'uniacid'=> $uniacid]);
    echo 'success';
    exit;
}

$submenus = [
    'xiaoqu'=> ['name'=> '小区', 'do'=> 'village'],
    'quan'=> ['name'=> '商圈', 'do'=> 'village'],
   
];

$citys = webHelper::citys();
//列表数据参数
 
$condition = ' WHERE `uniacid` = :uniacid  ';
$params = array(':uniacid' => $uniacid);
if(!empty($type)){
    $condition .= " AND `type` = :type";
    $params[':type'] = $type;
}
if(!empty($_GPC['area'])){
    $condition .= " AND `area` = :area";
    $params[':area'] =  trim($_GPC['area']); 
}
if(!empty($_GPC['keyword'])){
    $condition .= " AND `name` LIKE :name ";
    $params[':name'] =  "%". trim($_GPC['keyword']) ."%"; 
}
//获取列表数据
$fd = webHelper::table_list_data('kbwdl_area',$condition, $params);
$total = $fd['total'];
$list  = $fd['list'];
$pager = $fd['pager'];
//获取列表数据
 
//配置的系统参数
$sec = webHelper::fetch_place_data('setting', 'display');
$selects = $this->_forms;
$selects['areas'] =  $sec['areas'];
$selects['citys'] = webHelper::citys();
$selects['quan'] = webHelper::quans();

include $this->template('village/index');